﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Text;
using System.Web.Script.Serialization;
using Assignment_3.Classes;

namespace Assignment_3.Feature___Reports
{
    public partial class RoomStatusReport : System.Web.UI.Page
    {
        #region Control Methods
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null || Session["usertype"] == null)
            {
                Response.Redirect("~/Feature - Users/Login.aspx");
            }

            emptyLbl.Text = "No Results found";
        }

        protected void statusCbl_SelectedIndexChanged(object sender, EventArgs e)
        {
            emptyLbl.Text = "";
            List<ChartDisplay> chartdisplayList = new List<ChartDisplay>();
            ArrayList roomstatusList = new ArrayList();
            foreach (ListItem status in statusCbl.Items)
            {
                if (status.Selected)
                {
                    //Get records by status
                    ArrayList singlestatus = DatabaseManager.getAllRoomByStatus(status.Value);

                    //Populate all status records into a single arraylist
                    roomstatusList.AddRange(singlestatus);

                    //Pie Chart data
                    ChartDisplay cd = new ChartDisplay();
                    cd.Status = status.Value;
                    cd.Count = singlestatus.Count;
                    chartdisplayList.Add(cd);
                }
            }
            //Check if there are records
            if (roomstatusList.Count != 0)
            {
                chartPnl.Visible = true;

                //Table Chart
                StringBuilder data = new StringBuilder();
                JavaScriptSerializer json = new JavaScriptSerializer();
                json.Serialize(roomstatusList, data);
                hdData.Value = data.ToString();

                //Column Chart
                StringBuilder data2 = new StringBuilder();
                JavaScriptSerializer json2 = new JavaScriptSerializer();
                json2.Serialize(chartdisplayList, data2);
                hdData2.Value = data2.ToString();
            }
            else
            {
                chartPnl.Visible = false;
                emptyLbl.Text = "No Results found";
            }
        }
        #endregion
    }
}

#region other classes
public class ChartDisplay
{
    private string status;
    private int count;

    public string Status
    {
        get { return status; }
        set { status = value; }
    }

    public int Count
    {
        get { return count; }
        set { count = value; }
    }
}
#endregion